<?php
namespace App\Http\Controllers\Admin;

use App\admin\Adlog;
use App\admin\Technical;
use App\Http\Controllers\Admin\LogHander;
use App\Http\Controllers\Controller;
use App\Http\Middleware;
use Illuminate\Http\Request;
use Intervention\Image\ImageManagerStatic as Image;
use Illuminate\Routing\Controller as BaseController;

class TechnicalController extends BaseController
{
	public function __construct() {
		$this->middleware('admindo');
	}
	//查看列表
	public function index()
	{
		$flight = Technical::where('state',0)->orderBy('id', 'desc')->paginate(15);
		return view('admin.technicallist',['data' => $flight]);
	}
	
	//添加上传功能
	public function NewUp(Request $request)
	{
		$newName = $request->input('newName');
		$newAuthor = $request->input('newAuthor');
		$newFrom = $request->input('newFrom');
		$Description = $request->input('newDescription');
		$content = $request->input('content');
		$pathBase = "";

		if ($request -> hasFile('fileOne')) {
			$file = $request -> file('fileOne');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/technical/'. $name;
			Image::make($file) -> save($path);
			$pathBase = 'upload/technical/'. $name;
		}else{
			return redirect()->back();
		}

		$adminBackName = $request->session()->get('adminBackName');
		$Newnet = new Technical;
		$Newnet->name = $newName;
		$Newnet->description = $Description;
		$Newnet->author = $newAuthor;
		$Newnet->froms = $newFrom;
		$Newnet->info = $content;
		
		$Newnet->users = $adminBackName;//写入帐号
		$Newnet->imgurl = $pathBase;
		$Newnet->state = 0;
		if($Newnet->save()){
			$cars=array('name'=>'NewUp','info'=>'添加技术支持','users'=>$adminBackName,'cnt'=>$newName."---".$newAuthor."---".$newFrom);
			$temp = new LogHander;
			$temp->logs($cars);
			return redirect('TechnicalSee');
		}
	}
	
	public function Newadd(){
//		$back = array();
		$back['name'] = "";
		$back['description'] = "";
		$back['author'] = "";
		$back['froms'] = "";
		$back['info'] = "";
		$back['users'] = "";
		$back['imgurl'] = "";
//		dd($back);
		return view('admin.technicaladd',['data' => $back]);
	}
	public function Newdel(Request $request){
		
		$id = $request->input('id');
		$Newnet = new Technical;
		$back = $Newnet::find($id);
		$back->state = 1;
		if($back->save()){
			return "ok";
		}else{
			return "no";
		}
	}
	//新闻详情
	public function New_(Request $request){
		$id = $request->input('id');
		$Newnet = new Technical;
		$data = $Newnet::find($id);
		if(empty($data)){
			return "没有此数据";
		}else{
			return view('admin.technicals',['data' => $data]);
		}
	}
	//编辑
	public function NewEdit(Request $request){
		$id = $request->input('id');
		$Newnet = new Technical;
		$data = $Newnet::find($id);
		if(empty($data)){
			return "无此产品";
		}else{
			return view('admin.technicaledite',['data' => $data]);
		}
		
	}
	//编辑上传
	public function NewEditUp(Request $request){
		
		$Newnet = new Technical;
		$id = $request->input('id');
		if($id == ""){
			return "更新出错，找不到产品id";
		}
		
		$newName = $request->input('newName');
		$newAuthor = $request->input('newAuthor');
		$newFrom = $request->input('newFrom');
		$Description = $request->input('newDescription');
		$content = $request->input('content');
		$pathBase = "";

		if ($request -> hasFile('fileOne')) {
			$file = $request -> file('fileOne');
			$entension = $file -> getClientOriginalExtension();
			$ents = array("jpg", "jpge", "png", "bmp","gif");
			if(!in_array($entension, $ents)){
				return "只支持jpg, jpge, png, bmp,gif类型的图片";
			}
			$name = date("Ymd").str_random(10) . '.'.$entension;
			$path = public_path() .'/upload/technical/'. $name;
			Image::make($file) -> save($path);
			$pathBase = 'upload/technical/'. $name;
		}
		
		$temp = $Newnet::find($id);
		$adminBackName = $request->session()->get('adminBackName');
		
		$temp->name = $newName;
		$temp->description = $Description;
		$temp->author = $newAuthor;
		$temp->froms = $newFrom;
		$temp->info = $content;
		
		$temp->users = $adminBackName;//写入帐号
		$temp->imgurl = $pathBase;
		$temp->state = 0;
		if($temp->save()){
			$cars=array('name'=>'newediteUp','info'=>'编辑技术支持','users'=>$adminBackName,'cnt'=>$newName."---".$newAuthor."---".$newFrom);
			$tempx = new LogHander;
			$tempx->logs($cars);
			return redirect('TechnicalSee');
		}
	}
}
